package read.niit.dao

import read.niit.util.JDBCUtil

class Top10WithMinScoreDao {
  def insertTop10Data(top10Rdd: Array[((String, String), Int)]): Unit = {
    val conn = JDBCUtil.getConnection
    val insertQuery = "INSERT INTO minScoreTable(student_id, book_id, score) VALUES (?, ?, ?)"
    val pstmt = conn.prepareStatement(insertQuery)

    // 清空 MySQL 表数据
    val st = conn.createStatement()
    st.executeUpdate("TRUNCATE TABLE minScoreTable")

    // 迭代每一行并将它们插入到 MySQL 表中
    top10Rdd.foreach { row =>
      pstmt.setString(1, row._1._1)
      pstmt.setString(2, row._1._2)
      pstmt.setInt(3, row._2)
      pstmt.executeUpdate()
    }

    pstmt.close()
    conn.close()
  }
}
